﻿using System;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Collapse
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FuzzyGroupingInstruction:InstructionBase
    {
        #region props
        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private string _TableName;
        /// <summary></summary>
        public string TableName
        {
            get
            {
                return this._TableName;
            }
            set
            {
                this._TableName = value;
            }
        }

        private string _IDFieldName;
        /// <summary></summary>
        public string IDFieldName
        {
            get
            {
                return this._IDFieldName;
            }
            set
            {
                this._IDFieldName = value;
            }
        }

        private string _FieldName;
        /// <summary></summary>
        public string FieldName
        {
            get
            {
                return this._FieldName;
            }
            set
            {
                this._FieldName = value;
            }
        }

        private double _SimilarityThreshold;
        /// <summary></summary>
        public double SimilarityThreshold
        {
            get
            {
                return this._SimilarityThreshold;
            }
            set
            {
                this._SimilarityThreshold = value;
            }
        }

        private bool _PersistToTable;
        /// <summary></summary>
        public bool PersistToTable
        {
            get
            {
                return this._PersistToTable;
            }
            set
            {
                this._PersistToTable = value;
            }
        }

        private string _OutputTableName;
        /// <summary></summary>
        public string OutputTableName
        {
            get
            {
                return this._OutputTableName;
            }
            set
            {
                this._OutputTableName = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public FuzzyGroupingInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._ConnStr = string.Empty;
            this._TableName = string.Empty;
            this._IDFieldName = string.Empty;
            this._FieldName = string.Empty;
            this._SimilarityThreshold = 0.8;
            this._PersistToTable = false;
            this._OutputTableName = "tmp_FuzzyGroup";
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="connStr"></param>
        /// <param name="tblName"></param>
        /// <param name="idFieldName"></param>
        /// <param name="fieldName"></param>
        /// <param name="threshold">in the range of 0.1 to 0.9</param>
        /// <param name="persist">indicate whether to save output (similarity score and collapse id)</param>
        /// <param name="outputTblName">table name that hold the otuput</param>
        public FuzzyGroupingInstruction(Guid componentID, Guid jobTicketID,
            string connStr, string tblName, string idFieldName,
            string fieldName, double threshold,
            bool persist, string outputTblName) 
            : base(componentID, jobTicketID)
        {
            this._ConnStr = connStr;
            this._TableName = tblName;
            this._IDFieldName = idFieldName;
            this._FieldName = fieldName;
            this._SimilarityThreshold = threshold;
            this._PersistToTable = persist;
            this._OutputTableName = outputTblName;
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IDFieldName", this.IDFieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FieldName", this.FieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SimilarityThreshold", this.SimilarityThreshold.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PersistToTable", this.PersistToTable.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputTableName", this.OutputTableName);

            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FuzzyGroupingInstruction instruction = new FuzzyGroupingInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                instruction.TableName = XmlDataUtil.GetAttributeValue(xNode, "TableName", "");
                instruction.IDFieldName = XmlDataUtil.GetAttributeValue(xNode, "IDFieldName", "");
                instruction.FieldName = XmlDataUtil.GetAttributeValue(xNode, "FieldName", "");
                instruction.SimilarityThreshold =
                    double.Parse(XmlDataUtil.GetAttributeValue(xNode, "SimilarityThreshold", "0.8"));
                instruction.PersistToTable =
                    bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "PersistToTable", false.ToString()));
                instruction.OutputTableName = XmlDataUtil.GetAttributeValue(xNode, "OutputTableName", "");

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}
